<template>
  <div class="container">
    <!--标题-->
    <div style="margin-top: 100px;margin-left: 0px;">
      <div style="font-size: 30px;font-weight: bold">注册新用户</div>
      <div style="font-size: 14px;color:grey">欢迎注册，请在下方填写相关信息</div>
    </div>
    <!--表单-->
    <div style="margin-top: 20px;margin-left: 0px;">
      <el-form :model="form" :rules = 'rules' ref="form">
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" v-model="form.username" :maxlength="8" type="text" placeholder="用户名" style="width: 90%;">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input show-password prefix-icon="el-icon-lock" v-model="form.password" :maxlength="16" type="password" style="width: 90%;margin-top: 0px" placeholder="密码">
          </el-input>
        </el-form-item>
        <el-form-item prop="password_repeat">
          <el-input v-model="form.password_repeat" :maxlength="16" type="password" style="width: 90%;margin-top: 10px"
                    placeholder="再次输入密码" prefix-icon="el-icon-lock" show-password>
          </el-input>
        </el-form-item>
        <el-form-item prop="phone">
          <el-input prefix-icon="el-icon-phone-outline" v-model="form.phone" type="phone" style="width: 90%;margin-top: 10px" placeholder="电话号码">
          </el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input prefix-icon="el-icon-message" v-model="form.email" type="email" style="width: 90%;margin-top: 10px" placeholder="电子邮箱地址">
          </el-input>
        </el-form-item>
        <el-form-item prop="code">
          <div style="width: 90%;margin: 10px 0 0 20px">
            <el-row :gutter="10" style="width: 100%">
              <el-col :span="18">
                <el-input v-model="form.code" type="text" placeholder="请输入验证码">
                  <template #prefix>
                    <el-icon>
                      <EditPen/>
                    </el-icon>
                  </template>
                </el-input>
              </el-col>
              <el-col :span="6">
                <img ref="vcImg" src="/users/code" alt="验证码" @click="getVerifyCode">
              </el-col>
            </el-row>
          </div>
        </el-form-item>
      </el-form>
    </div>
    <!--注册按钮-->
    <div style="margin-top: 30px">
      <el-button style="width: 270px" type="warning" plain @click="registerU">注册</el-button>
    </div>
    <div style="margin-top: 20px">
      <span style="font-size: 14px;line-height: 15px;color: grey">已有帐号？</span>
      <el-link type="primary" style="translate: 0 -2px" @click="backToLogin">立即登录</el-link>
    </div>
  </div>
</template>

<script>
import { register } from '@/api/user'
import {getCode} from '@/api/user'
import { validUsername } from '@/utils/validate'
import axios from 'axios'


export default {
  name: 'Login',
  
  data() {
   
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('Please enter the correct user name'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('The password can not be less than 6 digits'))
      } else {
        callback()
      }
    }
    const checkRePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次确认密码'))
      } else if(value !== this.form.password) {
        callback(new Error('请确保两次输入的密码一样'))
      }
      else {
        callback()
      }
    }
    const checkCode = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入验证码'))
      } else if(value !== this.realCode) {
        this.getVerifyCode()
        callback(new Error('验证码输入错误'))
      }
      else {
        callback()
      }
    }
    return {
      form: {
        email: "",
        code: "",
        username: "",
        password: "",
        password_repeat: "",
        phone: ""
      },
      realCode:'',
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 6, max: 16, validator: validateUsername, trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, validator: validatePassword, trigger: 'blur' }
        ],
        password_repeat: [
          { validator: checkRePassword, trigger: 'blur' }
        ],
        code:[
          {validator:checkCode,trigger:'blur'}
        ]
      },
    }
  },
  watch: {

  },
  mounted:function(){
    this.getVerifyCode()
  },
  methods: {
    registerU() {
      this.$refs['form'].validate((valid)=>{
        if(valid){
          register(this.form).then((result) => {
          if (result.code === 200) {
            this.$message.success('注册成功，请前往邮箱激活账号后登陆')
            this.backToLogin()
          } 
          
        // console.log(this.form);
      })
        }else{
          return false
        }
      })
      
      
    },
    GetCode() {
      getCode().then((result) => {
  
        if (result.code === 200) {
          alert('验证码：'+ result.data)
          this.realCode = result.data
        } else {
          this.$message.error(result.msg);
        }
      })
    },
    // 重新获取表单验证码
    getVerifyCode () {
      // this.$refs.vcImg.src = '/users/code?uuid=' + Math.random()
    getCode().then(res => {
      if(res.code===200){
        console.log(res.data)
        this.realCode = res.data['code']
        let img = res.data['img']
        let imgBase64 = "data:image/jpeg;base64," + img
        this.$refs.vcImg.src = imgBase64
      }else {
          this.$message.error(res.msg);
        }
    })
    },
    backToLogin(){
      this.$router.push('/')
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

</style>

<style lang="scss" scoped>

.container{
  text-align: center;
  display: block;
  height: 100%;
  background-color: rgb(252, 252, 241);
}

// .el-input__inner {
//   background-color: rgb(252, 252, 241);
// }
</style>
